package com.logger;

import java.util.HashMap;
import java.util.Map;

/**
 * 日志打印等级
 * Created by chenxunlin01 on 2016/1/16.
 */

public class Level {

    public static final String LEVEL_VERBOSE = "verbose";
    public static final String LEVEL_DEBUG = "debug";
    public static final String LEVEL_INFO = "info";
    public static final String LEVEL_WARN = "warn";
    public static final String LEVEL_ERROR = "error";

    private String name;
    private int level;

    public static final Level VERBOSE = new Level(LEVEL_VERBOSE, 1001);
    public static final Level DEBUG = new Level(LEVEL_DEBUG, 1002);
    public static final Level INFO = new Level(LEVEL_INFO, 1003);
    public static final Level WARN = new Level(LEVEL_WARN, 1004);
    public static final Level ERROR = new Level(LEVEL_ERROR, 1005);

    private static Map<String, Level> levels;

    static {
        levels = new HashMap<String, Level>();
        levels.put(VERBOSE.getName(), VERBOSE);
        levels.put(DEBUG.getName(), DEBUG);
        levels.put(INFO.getName(), INFO);
        levels.put(WARN.getName(), WARN);
        levels.put(ERROR.getName(), ERROR);
    }

    public Level(String name, int level) {
        this.name = name;
        this.level = level;
    }


    /**
     * 比较两个等级，lv1>lv2,返回>0
     *
     * @param lv2
     * @return
     */
    public int compare(Level lv2) {
        return level - lv2.getLevel();
    }

    public int getLevel() {
        return level;
    }

    public String getName() {
        return name;
    }

    public static Level getLevel(String key) {
        return levels.get(key);
    }
}
